/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-02-8       金鹏       the first version
 */

#include "m100p.h"

extern struct CMN_UART uart_device[6];              /* 串口设备表 */
extern int cm_switch;                               /* 0:WIFI; 1:4G*/
extern int net_flag;                                /* 网络状态  0:初始状态 1:发送确认信息  2:接收到确认信息 3:已经断网*/
static rt_thread_t m100p_handl = RT_NULL;           /* 4G模块线程句柄*/

/*
 * M100P_Sent
 * @Param NULL
 *
 * Change Logs:
 * Date           Author     Notes
 * 2025-3-10       金鹏               4G模块发送
 */
void M100P_Sent(char buff[])
{
    int length = strlen(buff);
    for(int i=0;i<length;i++){
        rt_device_write(uart_device[4].uart_serial,0,&buff[i],1);
    }
}

/*
 * M100P_Entry
 * @Param NULL
 *
 * Change Logs:
 * Date           Author     Notes
 * 2025-2-8     金鹏        4G模块线程
 */
void M100P_Entry(void *parameter)
{
    char ch;
    static unsigned char buff[100] = "";
    short count=0;

    while(1){
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
        while (rt_device_read(uart_device[4].uart_serial, -1, &ch, 1) != 1)
        {
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(uart_device[4].uart_rx_sem, RT_WAITING_FOREVER);
        }

        /* 当4G开关打开时*/
        if(cm_switch == 1){
            if(ch != '}' && !rt_strstr(buff,",ok,")){
                //rt_kprintf("4g_log3 = %d\n",strlen(buff));
                buff[count++] = ch;
            }
            else if(rt_strstr(buff,",ok,")){
                buff[count++] = ch;
                buff[count] = '\0';

                net_flag = 2;
                //rt_kprintf("4g_log = %s\n",buff);
                rt_memset(buff, 0, sizeof(buff));

                count = 0;
            }
            else{
                //rt_kprintf("4g_log2 = %s\n",buff);
                buff[count++] = ch;
                buff[count] = '\0';

                char *ret = rt_strstr(buff,"@");

                if(ret != RT_NULL){
                    //rt_kprintf("4g = %s\n",ret);
                    command_deal(ret);
                }
                else{
                    //rt_kprintf("4g = %s\n",buff);
                }

                count = 0;
            }
        }
        else{
            rt_memset(buff, 0, sizeof(buff));
        }
    }
}

/*
 * M100P_Test
 * @Param NULL
 *
 * Change Logs:
 * Date           Author     Notes
 * 2025-2-8     金鹏        4G模块测试
 */
ENTRY M100P_Test(void)
{
    /* 4G初始化*/
    if( CMN_Uart_Init("uart5") != RT_EOK){
        rt_kprintf("M100P_Init failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("M100P_Init Successful\n");
    }

    /* 创建4G模块线程*/
    m100p_handl = rt_thread_create("4g",
        M100P_Entry,RT_NULL,
        ENTRY_STACK_SIZE-1024,
        ENTRY_PRI,
        ENTRY_TICK);

    /* LOG*/
    if(m100p_handl != RT_NULL){
        rt_kprintf("4g thread create successful!!\n");

        /* 启动4G模块线程*/
        rt_thread_startup(m100p_handl);
    }else{
        rt_kprintf("4g thread create failed!!\n");

        return -RT_ERROR;
    }
}

